const { wordBook } = require('../../utils/util');
// pages/english_word/english_word.js
const utils = require('../../utils/util');
const WordEntity = require('../beans/WordEntity').WordEntity;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    action: {method: ''},
    wordIndex: 0,
    currentWord: null,
    wordList:[
      {
        "symbol": "[ˌrevəˈlu:ʃənəri]",
        "sound": "https://v.ylapi.cn/img/api/api_reciteword_word_list/f9e1e7c2e7aca17f.mp3",
        "class_id": "45090",
        "name": "revolutionary",
        "course": "226",
        "class_title": "四级必备词汇",
        "desc": "n. 革命者，革命家"
      },
      {
          "symbol": "[pəˈziʃən]",
          "sound": "https://v.ylapi.cn/img/api/api_reciteword_word_list/03ea7405861f4b2b.mp3",
          "class_id": "45090",
          "name": "position",
          "course": "226",
          "class_title": "四级必备词汇",
          "desc": "n. 位置；职位；姿势"
      },
      {
          "symbol": "[ləˈbɔrətəri]",
          "sound": "https://v.ylapi.cn/img/api/api_reciteword_word_list/b9820b79652f922c.mp3",
          "class_id": "45090",
          "name": "laboratory",
          "course": "226",
          "class_title": "四级必备词汇",
          "desc": "n. 实验室，研究室，实验课"
      },
      {
          "symbol": "[reindʒ]",
          "sound": "https://v.ylapi.cn/img/api/api_reciteword_word_list/396e65ac416ce684.mp3",
          "class_id": "45090",
          "name": "range",
          "course": "226",
          "class_title": "四级必备词汇",
          "desc": "n. 范围；类别"
      },
      {
          "symbol": "[ˈpɜːsn]",
          "sound": "https://v.ylapi.cn/img/api/api_reciteword_word_list/c3b945de310deb7b.mp3",
          "class_id": "45090",
          "name": "person",
          "course": "226",
          "class_title": "四级必备词汇",
          "desc": "n. 人；个人"
      },
      {
          "symbol": "[naɪn]",
          "sound": "https://v.ylapi.cn/img/api/api_reciteword_word_list/ba0684e9671aedb1.mp3",
          "class_id": "45090",
          "name": "nine",
          "course": "226",
          "class_title": "四级必备词汇",
          "desc": "num. 九，九个"
      },
      {
          "symbol": "[ˈkʌpl]",
          "sound": "https://v.ylapi.cn/img/api/api_reciteword_word_list/bdc08849f8fdd4c3.mp3",
          "class_id": "45090",
          "name": "couple",
          "course": "226",
          "class_title": "四级必备词汇",
          "desc": "n. 夫妇；一对；几个"
      },
      {
          "symbol": "[ ˈsteitmənt]",
          "sound": "https://v.ylapi.cn/img/api/api_reciteword_word_list/4a063d84366732b4.mp3",
          "class_id": "45090",
          "name": "statement",
          "course": "226",
          "class_title": "四级必备词汇",
          "desc": "n. 陈述，声明"
      },
      {
          "symbol": "[əˈprəupriit]",
          "sound": "https://v.ylapi.cn/img/api/api_reciteword_word_list/5c28ce9a03f7e184.mp3",
          "class_id": "45090",
          "name": "appropriate",
          "course": "226",
          "class_title": "四级必备词汇",
          "desc": "adj. 适当的，恰当的"
      }
    ],
    msg: ''
  },

  audioPlay: function(e) {
    this.setData({
      action: {
        method: 'play'
      }
    });
  },
  onReady: function(e) {
    this.audioCtx = wx.createAudioContext('wordSound');
  },

  addFavrite: function(e) {
    let wordInfo = e.currentTarget.dataset.word_info;
    let level = 0;
    if(wordInfo.class_title.indexOf('四级') >= 0) {
      level = 4;
    } else if (wordInfo.class_title.indexOf('六级') >= 0) {
      level = 6;
    }
    let wordBookInfo = wx.getStorageSync(utils.wordBook);
    if(wordBookInfo == null || wordBookInfo == undefined || wordBookInfo == ''){
      wordBookInfo = [];
    }
    let entity = new WordEntity();
    entity.setWord(wordInfo.name);
    entity.setContent(wordInfo.desc);
    entity.setIsPass(false);
    entity.setLevel(level);
    entity.setSound(wordInfo.sound);
    entity.setSymbol(wordInfo.symbol);
    if(!utils.hasWordEntityOfArray(wordBookInfo,entity)) {
      wordBookInfo.push(entity);
    }else {
      utils.refushPassFlag4ArrayElement(wordBookInfo,entity.getWord(),entity.getLevel(),entity.getIsPass());
    }
    wx.setStorageSync(utils.wordBook, wordBookInfo);
  },

  wordPass: function(e) {
    let wordInfo = e.currentTarget.dataset.word_info;
    let level = 0;
    if(wordInfo.class_title.indexOf('四级') >= 0) {
      level = 4;
    } else if (wordInfo.class_title.indexOf('六级') >= 0) {
      level = 6;
    }
    let wordBookInfo = wx.getStorageSync(utils.wordBook);
    if(wordBookInfo == null || wordBookInfo == undefined || wordBookInfo == ''){
      wordBookInfo = [];
    }
    let entity = new WordEntity();
    entity.setWord(wordInfo.name);
    entity.setContent(wordInfo.desc);
    entity.setIsPass(true);
    entity.setLevel(level);
    entity.setSound(wordInfo.sound);
    entity.setSymbol(wordInfo.symbol);
    if(!utils.hasWordEntityOfArray(wordBookInfo,entity)) {
      wordBookInfo.push(entity);
    } else {
      utils.refushPassFlag4ArrayElement(wordBookInfo,entity.getWord(),entity.getLevel(),entity.getIsPass());
    }
    wx.setStorageSync(utils.wordBook, wordBookInfo);

    // 下一个单词
    this.nextWord();
  },

  /**
   * 下一个单词
   */
  nextWord: function() {
    this.data.wordIndex++;
    let wordBookInfo = wx.getStorageSync(utils.remoteWordList);
    console.log('next word',wordBookInfo);
    if(wordBookInfo !== null && wordBookInfo !== undefined && wordBookInfo !== '') {
      this.setData({currentWord: wordBookInfo[this.data.wordIndex]});
    }
    if(this.data.wordIndex > wordBookInfo.length) {
      wx.showToast({
        title: '人家也是有底线的，已经最后一个词啦！',
        icon: 'none',
        duration: 2000
      })
    }
  },
  /**
   * 初始化单词
   */
  initWord: function() {
    let wordBookInfo = wx.getStorageSync(utils.wordBook);
    if(wordBookInfo !== null && wordBookInfo !== undefined && wordBookInfo !== '') {
      this.data.currentWord = wordBookInfo[0];
    } else {
      wx.showToast({
        title: '还没有选择单词本',
        icon: 'success',
        duration: 2000
      })
    }
  },

  getWordFromRemote: function(options) {
    var course_num = parseInt(options.course_num);
    var class_id = parseInt(options.class_id);
    var that = this;
    setTimeout(() => {
      wx.request({
        method: 'POST',
        url: 'http://rw.ylapi.cn/reciteword/wordlist.u?uid=11422&appkey=055aa48e889bca3fe7f6a27702af80d8&' + 'class_id=' + class_id + '&course='+ course_num,
        // header: {
        //     'Content-Type': 'application/json'
        // },
        success: function(res) {
          console.log('ttt',res);
          if(res.data.code == 1000){
              that.setData({wordList: res.data.datas, currentWord: res.data.datas[0]});
              wx.setStorageSync(utils.remoteWordList, res.data.datas);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        },
        fail: function() {
          wx.showToast({
              title: '请求错误',
              icon: 'none',
              duration: 1500
          })
            console.log("error");
        },
      });
    },1000);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('onload = ',options);
    console.log('onload params of course_num = ', options.course_num);
    let obj = options.obj;
    let courseNum = options.course_num;
    if(courseNum !== null && courseNum !== undefined && !isNaN(courseNum)) {
      // options.class_id;
      this.getWordFromRemote(options);
    } 
    if (obj !== null && obj !== undefined) {
    console.log('onload options obj ',JSON.parse(options.obj));
      this.setData({
        currentWord: JSON.parse(options.obj)
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})